<!DOCTYPE >
<html>
	<html lang="ch">
	<head>
		<meta charset="utf-8">
		<title>欢迎！~</title>
		<style type="text/css">
			#title{
				font-family:"新宋体";
				position: absolute;
				color: brown;
				font-size: 60px;
			    text-align-last: center;
			    left: 40%;
			    top: 10%;
			}
			#content{
				position: absolute;
				top: 42%;/*顶部到元素*/
				left:30%;
				width: 40%;
				height: 680px;
				margin-top:-200;/*边缘到底部*/		
				background-color: #34495e;
				text-align-last: center;
				/*这里做一个半透明效果*/
				filter:alpha(Opacity=60);
				-moz-opacity:0.8;
				opacity: 0.8;
			}
			#checkbankcard{
				color: #3498db;
				font-size: 30px;
				text-align: center;
				border-radius: 25px;/*边框圆角*/
			}
			#srcbankcard{
				color: #3498db;
				font-size: 30px;
				text-align: center;
				border-radius: 25px;/*边框圆角*/
			}
			#dstbankcard{
				color: #3498db;
				font-size: 30px;
				text-align: center;
				border-radius: 25px;/*边框圆角*/
			}
			#amount{
				color: #3498db;
				font-size: 30px;
				text-align: center;
				border-radius: 25px;
			}
			#check{
				background-color: pink;
				border-radius:10px;
             	border:0;/*边框宽度0*/
            	height:50px;
             	width:90px;
            	padding:5px 10px;/*上下填充10 左右5*/
            	font-size: 20px;
			}
			#transfer{
				background-color: pink;
				border-radius:10px;
             	border:0;/*边框宽度0*/
            	height:50px;
             	width:90px;
            	padding:5px 10px;/*上下填充10 左右5*/
            	font-size: 20px;
			}
			#content h1{
				color: white;
				font-size: 40px;			
			}
         </style>
	</head>
	<body background="img/bg.png">
		<div id="title">
			<svg t="1585894995769" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3410" width="50" height="50"><path d="M0 0h1024v1024H0z" fill="#FFFFFF" p-id="3411"></path><path d="M212 80c-44.112 0-80 35.888-80 80v704c0 44.112 35.888 80 80 80h600c44.112 0 80-35.888 80-80V160c0-44.112-35.888-80-80-80H212m0-80h600c88.366 0 160 71.634 160 160v704c0 88.366-71.634 160-160 160H212c-88.366 0-160-71.634-160-160V160c0-88.366 71.634-160 160-160z" fill="#707070" p-id="3412"></path><path d="M322 12v1000a12 12 0 0 1-12 12h-56a12 12 0 0 1-12-12V12a12 12 0 0 1 12-12h56a12 12 0 0 1 12 12z" fill="#707070" p-id="3413"></path><path d="M678 80v199.11h88V80h-88m-60-60h208v283.652c0 19.582-17.908 35.458-40 35.458h-128c-22.092 0-40-15.876-40-35.458V20z" fill="#707070" p-id="3414"></path></svg>
			狗爷银行	
		</div>
		<div id="content">		
			<h1 >查询余额业务</h1><br />
			<form action="#"method="get" >
				<input type="text" name="checkbankcard" id="checkbankcard" placeholder="请输入要查询的银行卡号" /><br /><br />
				<input type="button" name="check" id="check" value="查询余额" onclick="check_balance()"/>
			</form>
			<h1 >转账业务</h1><br />
			<form action="#"method="post" >
				<input type="text" name="bankcard" id="srcbankcard" placeholder="请输入要出账的银行卡号" /><br /><br />
				<input type="text" name="bankcard" id="dstbankcard" placeholder="请输入要入账的银行卡号" /><br /><br />
				<input type="text" name="amount" id="amount" placeholder="请输入金额(单位：元)" /><br /><br />
				<input type="button" name="transfer" id="transfer" value="转账" onclick="transfer_accounts()"/>
			</form>
			<input type="button" name="transfer" id="transfer" value="退出账户" onclick="exit_account()"/>
		</div>
	</body>
	<script>
        function getCookie(cookieName) {
            const strCookie = document.cookie
            const cookieList = strCookie.split(';')
            for(let i = 0; i < cookieList.length; i++) {
                const arr = cookieList[i].split('=')
                if (cookieName === arr[0].trim()) {
                    return arr[1];
                }
            }
            return ''
        }
        function check_balance()
        {
            // 获取信息
            var checkbankcard = document.getElementById("checkbankcard");
            if(checkbankcard.length == 0)
            {
                alert("查询的银行卡号不能为空");
            }
            var objStr = "uid=" + checkbankcard.value;
            var url = "http://139.9.84.117:5000/balance/get?" + objStr;
            //alert(url);
            var httpRequest = new XMLHttpRequest();
	        httpRequest.withCredentials = true;
            httpRequest.open('GET', url);
            httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            httpRequest.send();
            // 回调函数
            httpRequest.onreadystatechange = function()
            {
		        //alert(httpRequest.readyState);
                if(httpRequest.readyState == 4 && httpRequest.status == 200)
                {
					//alert(document.cookie);
					var responseText = httpRequest.responseText;
					//alert(responseText);
					// httpRequest.withCredentials = true;
					// alert(httpRequest.getResponseHeader('set-cookie'));
					if(responseText.trim() == "Get balance failed(not your account)")
                    {
                        alert("不是你的账户");
                    }
                    else if(responseText.trim() == "Get balance failed(uid not exist)")
                    {
                        alert("没有这个账户");
                    }
                    else if(responseText.trim() == "session expired")
                    {
                        alert("cookie过期了捏");
						window.location.href = "login.html";
                    }
                    else
                    {
						let realAmount = parseInt(responseText.trim() / 10) / 100;
                        let balanceStr = "余额为" + realAmount + "!\n"
                        let r = confirm(balanceStr);
					}
                }
            }
        }
        function transfer_accounts()
        {
            // 打开http post请求
            var url = "http://139.9.84.117:5000/balance/transfer";
            var httpRequest = new XMLHttpRequest();
	        httpRequest.withCredentials = true;
            httpRequest.open('POST', url, true);
	    // httpRequest.setRequestHeader("Access-Control-Allow-Credentials","true");
            httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    // var meta = document.createElement('meta');
	    // meta.name = 'referrer';
	    // meta.content = 'unsafe-url';
	    // document.head.appendChild(meta);
            // 获取信息
            var srcbankcard = document.getElementById("srcbankcard");
            var dstbankcard = document.getElementById("dstbankcard");
            var amount = document.getElementById("amount");
            if(srcbankcard.length == 0 || dstbankcard.length == 0 || amount.length == 0)
            {
                alert("银行卡号和金额均不能为空");
            }
			let realAmount = parseInt(amount.value * 1000);
            var objStr = "src_uid=" + srcbankcard.value + "&dst_uid=" + dstbankcard.value + "&amount=" + realAmount;
            //alert(objStr);
            httpRequest.send(objStr);
            // 回调函数
            httpRequest.onreadystatechange = function()
            {
		        //alert(httpRequest.readyState);
                if(httpRequest.readyState == 4 && httpRequest.status == 200)
                {
					//alert(document.cookie);
					var responseText = httpRequest.responseText;
					//alert(responseText);
					// httpRequest.withCredentials = true;
					// alert(httpRequest.getResponseHeader('set-cookie'));
					if(responseText.trim() == "Transfer balance failed(amount should be positive)")
                    {
                        alert("不能使用负数金额");
                    }
                    else if(responseText.trim() == "Transfer balance failed(src_uid not exist)")
                    {
                        alert("没这个源账户逼");
                    }
                    else if(responseText.trim() == "Transfer balance failed(dst_uid not exist)")
                    {
                        alert("没这个目的账户");
                    }
                    else if(responseText.trim() == "Transfer balance failed(Insufficient account balance)")
                    {
                        alert("这个账户没钱啦");
                    }
                    else if(responseText.trim() == "session expired")
                    {
                        alert("cookie过期了");
						window.location.href = "login.html";
                    }
                    else
                    {
                        let balanceStr = "转账成功了！"
                        let r = confirm(balanceStr);
					}
                }
            }
        }

		function exit_account()
		{
			var exp = new Date();
			exp.setTime (exp.getTime() - 1);
			document.cookie = "sessionID=" + getCookie("sessionID") + "; expires=" + exp.toGMTString();
			window.location.href = "login.html";
		}
	</script>
</html>	 	
